package com.youease.chris.view.mediator
{
	import com.youease.chris.control.MapSceneCommand;
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.manager.OperateEnum;
	import com.youease.chris.manager.event.LayerItemEvent;
	import com.youease.chris.model.proxy.MapDataProxy;
	import com.youease.chris.model.vo.LayerData;
	import com.youease.chris.model.vo.TextureData;
	import com.youease.chris.view.ToolPanel;
	
	import mx.collections.ArrayCollection;
	import flash.events.MouseEvent;
	import mx.events.ListEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ToolPanelMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ToolMediator";
		public static const SHOW_LAYER:String = "ShowLayer";
		public static const LOCK_LAYER:String = "LockLayer";
		
		public function ToolPanelMediator(viewComponent:ToolPanel)
		{
			super(NAME, viewComponent);
			toolPanel.addLayer.addEventListener(MouseEvent.CLICK,onAddLayer);
			toolPanel.deleteLayer.addEventListener(MouseEvent.CLICK,onDeleteLayer);
			toolPanel.layersList.addEventListener(ListEvent.ITEM_CLICK,onChangeLayer);
			toolPanel.addEventListener(SHOW_LAYER,onShowLayer);
			toolPanel.addEventListener(LOCK_LAYER,onLockLayer);
		}
		
		private function onShowLayer(evt:LayerItemEvent):void {
			this.sendNotification(SHOW_LAYER,{layerName:evt.layerName,flag:evt.flag});
		}
		
		private function onLockLayer(evt:LayerItemEvent):void {
			this.sendNotification(LOCK_LAYER,{layerName:evt.layerName,flag:evt.flag});
		}
		
		private function onAddLayer(evt:MouseEvent):void {
			this.sendNotification(MapSceneCommand.CREATE_SCENE,null,MapSceneCommand.CREATE_SCENE_TYPE_ADDLAYER);
		}
		
		private function onDeleteLayer(evt:MouseEvent):void {
			if(toolPanel.layersList.dataProvider.length>1&&toolPanel.layersList.selectedItem)
				this.sendNotification(MapSceneCommand.CREATE_SCENE,toolPanel.layersList.selectedItem,MapSceneCommand.CREATE_SCENE_TYPE_DELLAYER);
		}
		
		private function onChangeLayer(evt:ListEvent):void {
			GlobalObject.currentLayer = toolPanel.layersList.selectedItem.name as String;
			trace(GlobalObject.currentLayer+"--------");
		}
		
		override public function listNotificationInterests():Array {
			return [
				MapSceneCommand.CREATE_SCENE_LAYER_CHANGED
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case MapSceneCommand.CREATE_SCENE_LAYER_CHANGED:
					var arr:ArrayCollection = new ArrayCollection();
					for each(var layer:LayerData in (this.facade.retrieveProxy(GlobalObject.currentMap) as MapDataProxy).mapData.layers) {
						arr.addItem(layer);
					}
					toolPanel.layersList.dataProvider = arr;
					toolPanel.layersList.selectedIndex = 0;
					break;
			}
		}
		
		public function get toolPanel():ToolPanel {
			return this.viewComponent as ToolPanel;
		}
	}
}